package com.wswy.wzcx.ui.dialog

import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatDialog
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.gone
import com.wswy.wzcx.module.base.onClick
import com.wswy.wzcx.module.base.visible
import kotlinx.android.synthetic.main.dialog_warning_alert.*

class WarningDialog(context: Context,private val msg:CharSequence) :  AppCompatDialog(context, R.style.customTransparentDialog2) {

    var clickListener : View.OnClickListener?=null

    var cancelClickListener : View.OnClickListener?=null

    var textConfigure : ((TextView)->Unit?)?=null

    var actionDismiss = true

    var buttonText :String?=null
    var cancelText:String?=null
    var title:String?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_warning_alert)
        setCancelable(false)
        setCanceledOnTouchOutside(false)

        textConfigure?.invoke(tv_message)

        if (!TextUtils.isEmpty(title)){
            tv_title.text = title
        }
        if (!TextUtils.isEmpty(buttonText)){
            tv_ok.text = buttonText
        }
        if (TextUtils.isEmpty(cancelText)){
            tv_cancel.gone()
            space.gone()
        }else{
            tv_cancel.visible()
            space.visible()
            tv_cancel.text = cancelText
            tv_cancel.onClick {
                if (actionDismiss){
                    dismiss()
                }

                cancelClickListener?.onClick(it)
            }

        }

        tv_message.text = msg

        tv_ok.onClick {
            if (actionDismiss){
                dismiss()
            }
            clickListener?.onClick(it)
        }

    }


    fun updateText(title:String?,msg: CharSequence?,cancelText:String?,okText:String?){
        this.title = title

        this.cancelText = cancelText
        buttonText = okText

        tv_message.text = msg

        if (!TextUtils.isEmpty(title)){
            tv_title.text = title
        }
        if (!TextUtils.isEmpty(okText)){
            tv_ok.text = okText
        }
        if (TextUtils.isEmpty(cancelText)){
            tv_cancel.gone()
            space.gone()
        }else{
            tv_cancel.visible()
            space.visible()
            tv_cancel.text = cancelText
        }

    }

}